package com.lc.afbx.adapters;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiShowICompany;
import com.zcx.helper.util.UtilLog;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/6/23.
 */
public class CompanyAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<GetApiShowICompany.Data> dataList;
    public HashMap<Integer, Boolean> isCheck;
    public Context context;

    public CompanyAdapter(Context context, List<GetApiShowICompany.Data> dataList) {
        inflater = LayoutInflater.from(context);
        this.dataList = dataList;
        this.context = context;

        isCheck = new HashMap<>();
        for (int i = 0; i < dataList.size(); i++) {
            isCheck.put(i, false);
        }

    }

    public void init() {
//        UtilLog.e("adapter里", "看这里走没走啊");
        for (int i = 0; i < dataList.size(); i++) {
            isCheck.put(i, false);
        }
    }

    @Override
    public int getCount() {

        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_company, null);
            BaseApplication.ScaleScreenHelper.loadView((ViewGroup) view);

            view.setBackgroundColor(context.getResources().getColor(R.color.white));

            holder = new ViewHolder();

            holder.img = (SimpleDraweeView) view.findViewById(R.id.img);
            holder.companyName = (TextView) view.findViewById(R.id.companyName);
            holder.gift = (TextView) view.findViewById(R.id.gift);
            holder.check = (CheckBox) view.findViewById(R.id.img_check);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.check.setChecked(isCheck.get(position));

        holder.img.setImageURI(Uri.parse(dataList.get(position).picurl));

        holder.companyName.setText(dataList.get(position).title);
        holder.gift.setText(dataList.get(position).privilege);

        return view;
    }

    class ViewHolder {
        SimpleDraweeView img;
        TextView companyName;
        TextView gift;
        CheckBox check;
    }
}
